<?php

/**
 * User Class
 * @package mobi.application.modules.admin
 * @author xoannt
 */
class User extends MY_Controller{
    protected $table = 'tbl_user';
    protected $col_id = 'user_id';
    /**
 * construct
 * 
 * @access public
 */
    public function __construct() {
        parent::__construct();
        $this->load->model('user_model');
    }
/**
 * manage user
 * 
 * @access public
 */
    public function index()
    {
        $data['list_user'] = $this->user_model->list_tab($this->table);
        $this->layout('user/index', $data);
    }
/**
 * validate form: check empty, check unique username, check format email
 * 
 * @access public
 */
    public function set_validate($id)
    {
        if(isset($id) && $id != '')
        {
            $this->form_validation->set_rules('txtname', 'Username', 'required|is_unique[tbl_user.user_name.user_id.'.$id.']');  
        }
        else
        {
            $this->form_validation->set_rules('txtname', 'Username', 'required|is_unique[tbl_user.user_name]');
        }
        $this->form_validation->set_rules('txtpass', 'Password', 'required|md5'); 
        $this->form_validation->set_rules('txtemail', 'Email', 'required|valid_email');  
        $this->form_validation->set_message('required', '%s  is not empty');
        $this->form_validation->set_message('valid_email', '%s  is not format');
    } 
/**
 * add user
 * 
 * @access public
 */
    public function insert()
    {
        $this->set_validate('');
        $username = $this->input->post('txtname'); $password = $this->input->post('txtpass'); $email = $this->input->post('txtemail');
        $pass = md5($password);
        $data_input = array( 'user_name' => $username,'user_password' => $pass,'user_email' => $email);
        $data = $this->check_form->check_sql($data_input);
        if($this->form_validation->run()=== FALSE)
        {
            $this->layout('user/insert',$data);
            
        }
        else
        {
            
            $this->user_model->action_tab($this->table, $data, '');
            redirect('admin/user');      
        }
    }
/**
 * edit information user
 * 
 * @access public
 */
    public function edit($id = '')
    {
       if(isset($id) && $id != '')
        {
            $this->set_validate($id);
            $username = $this->input->post('txtname'); $password = $this->input->post('txtpass'); $email = $this->input->post('txtemail');
            $pass = md5($password);
            $data_input = array( 'user_name' => $username,'user_password' => $pass,'user_email' => $email);
            $data = $this->check_form->check_sql($data_input);
            
            if($this->form_validation->run()=== FALSE)
            {
                $data['get_one'] = $this->user_model->get_one($this->table, $this->col_id, $id);
                $this->layout('user/edit',$data);
            }
            else
            {
                $this->user_model->action_tab($this->table, $data, $this->col_id, $id);
                redirect('admin/user');        
            }
        }
       
    }
/**
 * delete one user
 * 
 * @access public
 */
    public function delete($id = '')
    {
        if(isset($id) && $id != '')
        {
           $this->user_model->detele_one($this->table, $this->col_id, $id); 
           redirect('admin/user'); 
        }
    }
    



/**
 * index admin
 * 
 * @access public
 * @param type $name Description
 * @return type Description
 */
}
 
